package com.hhb.test;

import com.hhb.mapper.UserMapper;
import com.hhb.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class Demo04 {

    String resource = "mybatis-config.xml";
    InputStream inputStream = null;
    SqlSession openSession = null;



    @Test
    public void test01()  {


        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            openSession = sqlSessionFactory.openSession();

            UserMapper mapper = openSession.getMapper(UserMapper.class);

            List<User> users = mapper.findUserLikeUsername("%黄陈%");
            for(User user:users){
                System.out.println(user);
            }
//            System.out.println(users);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            openSession.close();
        }

    }

    @Test
    public void test02()  {


        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            openSession = sqlSessionFactory.openSession();

            UserMapper mapper = openSession.getMapper(UserMapper.class);

            Map<String, Object> map = mapper.findUserReturnMap(33);
            System.out.println(map);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            openSession.close();
        }

    }

    @Test
    public void test03()  {


        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

            openSession = sqlSessionFactory.openSession();

            UserMapper mapper = openSession.getMapper(UserMapper.class);

            Map<String, User> map = mapper.findUsersReturnMapByUsername("%黄陈%");
            System.out.println(map);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            openSession.close();
        }

    }
}
